package com.example.restatr.Atcivity.PetHospital;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.Adapter;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.example.restatr.Atcivity.Main.MainActivity;
import com.example.restatr.Atcivity.PetHospital.Bean.MyConsultationBean;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class MyConsultationAt extends AppCompatActivity {
    private Toolbar toolbar;
    private TextView barTitle;
    private EditText search;
    private RecyclerView rv;
    private List<MyConsultationBean.RowsBean> rowsBeans = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_my_consultation_at );

        toolbar = findViewById( R.id.toolbar );
        barTitle = findViewById( R.id.bar_Title );
        search = findViewById( R.id.search );
        rv = findViewById( R.id.rv );

        Tools.SetBarTitle( toolbar, barTitle, "我的咨询", search, this, false );
        GetMyAdvice();
    }

    private void GetMyAdvice() {
        new Thread( () -> {
            String token = Tools.Get( "/prod-api/api/pet-hospital/inquiry/my-list", MainActivity.sp.getString( "token", "" ) );
            MyConsultationBean myConsultationBean = new Gson().fromJson( token, MyConsultationBean.class );
            rowsBeans = myConsultationBean.getRows().stream().filter( new Predicate<MyConsultationBean.RowsBean>() {
                @Override
                public boolean test(MyConsultationBean.RowsBean rowsBean) {
                    return rowsBean.getDoctor() != null;
                }
            } ).collect( Collectors.toList() );
            runOnUiThread( () -> {
                if (rowsBeans.size() > 0) {
                    rv.setAdapter( new adapter() );
                }
            } );
        } ).start();
    }

    class adapter extends Adapter<adapter.viewHolder> {

        class viewHolder extends ViewHolder {
            private TextView title;
            private ImageView rightTriangle;
            private TextView title2;
            private TextView undertaker;
            private LinearLayout ll2;
            private TextView content;
            private ImageView img;

            public viewHolder(@NonNull View itemView) {
                super( itemView );
                title = itemView.findViewById( R.id.title );
                rightTriangle = itemView.findViewById( R.id.rightTriangle );
                title2 = itemView.findViewById( R.id.title2 );
                undertaker = itemView.findViewById( R.id.undertaker );
                ll2 = itemView.findViewById( R.id.ll2 );
                content = itemView.findViewById( R.id.content );
                img = itemView.findViewById( R.id.img );
                rv = itemView.findViewById( R.id.rv );
            }
        }

        @NonNull
        @Override
        public viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = getLayoutInflater().inflate( R.layout.service_info_item, parent, false );
            return new viewHolder( inflate );
        }

        @Override
        public void onBindViewHolder(@NonNull viewHolder holder, int position) {
            holder.ll2.setVisibility( View.GONE );
            holder.title.setText( "用户" + rowsBeans.get( position ).getUserId() );
            holder.rightTriangle.setVisibility( View.VISIBLE );
            holder.title2.setVisibility( View.VISIBLE );
            holder.title2.setText( rowsBeans.get( position ).getDoctor().getName() + "医生" );
            holder.undertaker.setText( rowsBeans.get( position ).getCreateTime().substring( 0, 10 ) );
            holder.content.setText( rowsBeans.get( position ).getQuestion() );
            Glide.with( MyConsultationAt.this )
                    .load( Tools.ip + rowsBeans.get( position ).getImageUrls() )
                    .transform( new RoundedCorners( 15 ) )
                    .listener( new RequestListener<Drawable>() {
                        @Override
                        public boolean onLoadFailed(@Nullable GlideException e, Object o, Target<Drawable> target, boolean b) {
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(Drawable drawable, Object o, Target<Drawable> target, DataSource dataSource, boolean b) {
                            holder.img.setImageDrawable( drawable );
                            holder.img.setVisibility( View.VISIBLE );
                            return true;
                        }
                    } )
                    .into( holder.img );
            holder.itemView.setOnClickListener( new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent( MyConsultationAt.this, PetDoctorDetailsAt.class );
                    intent.putExtra( "data", new Gson().toJson( rowsBeans.get( position ).getDoctor() ) );
                    startActivity( intent );
                }
            } );
        }

        @Override
        public int getItemCount() {
            return rowsBeans.size();
        }

    }

}